//
//  JwBaseViewController.m
//  JwPack
//
//  Created by 陈警卫 on 2022/8/25.
//

#import "JwBaseViewController.h"

@interface JwBaseViewController ()

@end

@implementation JwBaseViewController

- (void)dealloc {
    DLog(@"%@--dealloc", self);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.edgesForExtendedLayout = UIRectEdgeAll;
    // self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
    kJwWeak(self)
    self.view.jw_didSkinerBlock = ^(id  _Nonnull sender) {
        [wself jw_skinerBlockAction:sender];
    };
}

/// MARK: 全局项目刷新回调
/// 解决 jw_skinerBlockAction 父类子类实现冲突
- (void)jw_skinerBlockAction:(id)sender {
    self.view.backgroundColor = [UIColor jw_colorHexString:GV_JwFrame_backColorString];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    /// 设置导航栏状态
    [self setupNavBarStyle];
    /// 设置导航栏颜色
    [self setupNavBarApperance];
    
    if ([self isBeingPresented] || [self isMovingToParentViewController]) {
        // push/present
    } else {
        // pop/dismiss to here
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    if ([self isBeingDismissed] || [self isMovingFromParentViewController]) {
        // pop/dismiss
    } else {
        // push/present from here
    }
}

/// 设置导航栏状态 (子类viewDidLoad中设置)
- (void)setupNavBarStyle {
    if (self.jw_navBarStyle == JwNavBarHidden) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    } else if (self.jw_navBarStyle == JwNavBarShow) {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    } else if (self.jw_navBarStyle == JwNavBarDefault) {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}

/// 设置导航栏颜色
- (void)setupNavBarApperance {
    UIColor *backColor = self.jw_navBarBackColor;
    UIColor *tintColor = self.jw_navBarTintColor;
    
    if (!backColor) {
        backColor = [UIColor jw_colorHexString:GV_JwFrame_navBarBackColorString];
    }
    if (!tintColor) {
        tintColor = [UIColor jw_colorHexString:GV_JwFrame_navBarTintColorString];
    }
    [self setupNavBarApperanceWithBackColor:backColor tintColor:tintColor];
}

/// 设置导航栏颜色 (子类viewWillAppear中设置)
- (void)setupNavBarApperanceWithBackColor:(UIColor *)backColor tintColor:(UIColor *)tintColor {
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        [appearance configureWithTransparentBackground];
        appearance.backgroundColor = backColor;
        appearance.shadowColor = [UIColor clearColor];
        appearance.titleTextAttributes = @{NSForegroundColorAttributeName: tintColor};
        self.navigationController.navigationBar.standardAppearance = appearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
    } else {
        self.navigationController.navigationBar.translucent = NO;
        self.navigationController.navigationBar.barTintColor = backColor;
        self.navigationController.navigationBar.tintColor = tintColor;
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: tintColor};
        
        // 去掉底边阴影线
        [self.navigationController.navigationBar setShadowImage:[UIImage new]];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    }
}

@end
